package com.yb.xczx.cloud.auth.bean.vo;

import com.yb.xczx.cloud.auth.entity.SysUser;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/18 17:14
 * FileName: XczxSysUserVo
 * Description:
 */

@Data
public class XczxSysUserVo {

    private SysUser sysUser;

    private String password;

    private  String username;

    private Collection<GrantedAuthority> authorities;

    private  boolean accountNonExpired;

    private  boolean accountNonLocked;

    private  boolean credentialsNonExpired;

    private  boolean enabled;

    private String loginAccessToken;

    private String oauth2AccessToken;

    private String loginType;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Authority{
        private String authority;
    }
    public void setAuthorities(Set<Authority> authorities) {
        if(CollectionUtils.isEmpty(authorities)){
            this.authorities= Collections.emptySet();
            return;
        }
        Set<String> sets = authorities.stream().map(Authority::getAuthority).collect(Collectors.toSet());
        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList(sets.toArray(new String[0]));
        this.authorities = authorityList;
    }

}
